package com.relytosoft.mizar.media.analyze.services.service.system.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyPlanTemplate;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyPlanTemplateVo;
import com.relytosoft.mizar.media.analyze.services.mapper.system.AyPlanTemplateMapper;
import com.relytosoft.mizar.media.analyze.services.service.system.AyPlanTemplateService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lj
 * @date 2022/5/12 11:14
 */
@Service
public class AyPlanTemplateServiceImpl extends ServiceImpl<AyPlanTemplateMapper, AyPlanTemplate> implements AyPlanTemplateService {

    @Override
    public IPage<AyPlanTemplate> selectAyPlanTemplatePageList(PageQuery pageQuery, AyPlanTemplate ayPlanTemplate) {
        ayPlanTemplate.setStatus(1);
        if (StrUtil.isBlank(pageQuery.getOrderByColumn())) {
            pageQuery.setOrderByColumn("id");
            pageQuery.setIsAsc("desc");
        }
        return baseMapper.selectPageTemplateList(pageQuery.build(), ayPlanTemplate);
    }

    @Override
    public boolean addAyPlanTemplate(AyPlanTemplate ayPlanTemplate) {
        ayPlanTemplate.setStatus(1);
        return this.save(ayPlanTemplate);
    }

    @Override
    public boolean updateAyPlanTemplate(AyPlanTemplate ayPlanTemplate) {
        return this.updateById(ayPlanTemplate);
    }

    @Override
    public AyPlanTemplateVo selectAyPlanTemplateById(Integer templateId) {
        AyPlanTemplate ayPlanTemplate = this.getById(templateId);
        return BeanCopyUtils.copy(ayPlanTemplate, AyPlanTemplateVo.class);
    }

    @Override
    public boolean updateAyPlanTemplateStatus(List<Integer> templateIds) {

        AyPlanTemplate ayPlanTemplate = new AyPlanTemplate();
        ayPlanTemplate.setStatus(0);
        LambdaQueryWrapper<AyPlanTemplate> wrapper = new QueryWrapper<AyPlanTemplate>().lambda();
        wrapper.eq(AyPlanTemplate::getStatus, 1).in(AyPlanTemplate::getId, templateIds);
        return  this.update(ayPlanTemplate, wrapper);
    }
}
